*"* use this source file for the definition and implementation of
*"* local helper classes, interface definitions and type
*"* declarations
CLASS zcl_stock_get DEFINITION.
  PUBLIC SECTION.

* Attributes
    DATA:lt_matnr TYPE TABLE FOR CREATE i_producttp_2,
         ls_matnr TYPE i_producttp_2.
    DATA:lv_msg_text TYPE string.

    "库存查询
    METHODS  stock_sel
      IMPORTING
                is_data      TYPE ty_json
      RETURNING VALUE(e_msg) TYPE ty_msg.

ENDCLASS.

CLASS zcl_stock_get IMPLEMENTATION.

  METHOD stock_sel.
    DATA:lt_tab TYPE TABLE OF ty_tab.
    DATA:lt_stock  TYPE TABLE OF ty_stock2,
         ls_stock  TYPE ty_stock2,
         lt_stock2 TYPE TABLE OF ty_stock2,
         ls_stock2 TYPE ty_stock2,
         lt_data   TYPE TABLE OF t_data,
         ls_data   TYPE t_data.
    DATA:lv_text   TYPE string.

    SELECT a~product,
           a~plant,
           a~storagelocation,
           a~matlwrhsstkqtyinmatlbaseunit AS stock,
           a~materialbaseunit AS unit,
           a~productgroup,
           a~producttype,
           b~productname,
           c~storagelocationname
      FROM i_stockquantitycurrentvalue_2( p_displaycurrency = 'CNY' ) AS a
      JOIN i_producttext AS b ON a~product = b~product
      JOIN i_storagelocation AS c ON a~storagelocation = c~storagelocation
      WHERE b~language = '1'
      INTO TABLE @lt_tab.

    DELETE lt_tab WHERE stock IS INITIAL.
    MOVE-CORRESPONDING lt_tab TO lt_stock.
    SORT lt_stock BY product plant storagelocation stock.
    DELETE ADJACENT DUPLICATES FROM lt_stock COMPARING product plant storagelocation stock.
    LOOP AT lt_stock INTO ls_stock.
      COLLECT ls_stock INTO lt_stock2.
      CLEAR:ls_stock.
    ENDLOOP.

    SORT lt_tab BY product plant.
    DELETE ADJACENT DUPLICATES FROM lt_tab COMPARING product plant.

    DATA:lt_sto TYPE TABLE OF t_stock,
         ls_sto TYPE t_stock.
    LOOP AT lt_tab ASSIGNING FIELD-SYMBOL(<fs>).
      ls_data-itemcode = <fs>-product.
      ls_data-itemname = <fs>-productname.
      ls_data-whscode  = <fs>-plant.

      LOOP AT lt_stock2 INTO ls_stock2 WHERE product = <fs>-product AND plant = <fs>-product.
        ls_sto-whscode = ls_stock2-storagelocation.
        ls_sto-whsname = ls_stock2-storagelocationname.
        ls_sto-quantity = ls_stock2-stock.
        APPEND ls_sto TO lt_sto.
        CLEAR:ls_sto.
      ENDLOOP.

      ls_data-stocklist = lt_sto.
      CLEAR:lv_text.
      CONCATENATE '查询物料' ls_data-itemcode  '库存数据成功' INTO lv_text.
      e_msg-state = '0'.
      e_msg-errormsg = lv_text.
      e_msg-data = lt_data.
    ENDLOOP.
  ENDMETHOD.
ENDCLASS.
